<?php
    
    // TITLE: Address Model
    // FILE: app/shared/model/address.php

    
    $createAddress = function($id, $street1, $street2, $street3, $city, $stateId, $zip) {
	    global $con;
	    $sql = 'INSERT INTO address (id, street_1, street_2, street_3, city, state_id, zip) VALUES (' . $id . ', ' . $street1 . ', ' . $street2 . ', ' . $street3 . ', ' . $city . ', ' . $stateId . ', ' . $zip . ')';
	    $this->open();
	    $result = mysqli_query($con, $sql);
	    $id = ($result) ? mysqli_insert_id($con) : $result;
	    $this->close();
	    return $id;
    };// end function

    $readAddress = function($id) {
	    global $con;
	    $sql = 'SELECT * FROM address WHERE id = ' . $id;
	    $this->open();
	    $results = mysqli_query($con, $sql);
	    $this->close();
	    if ($results) {
		    $result = mysqli_fetch_array($results);
		    $address = new Address();
		    $address->setId($result[0]);
		    $address->setStreet1($result[1]);
		    $address->setStreet2($result[2]);
		    $address->setStreet3($result[3]);
		    $address->setCity($result[4]);
		    $address->setStateId($this->readState($result[5]));
		    $address->setZip($result[6]);
	    } else {
		    $address = false;
	    }
	    return $address;
    };// end function

    $updateAddress = function($id, $street1, $street2, $street3, $city, $stateId, $zip) {
	    global $con;
	    $sql = 'UPDATE address SET street_1 = ' . $street_1 . ', street_2 = ' . $street2 . ', street_3 = ' . $street3 . ', city = ' . $city . ', state_id = ' . $stateId . ', zip = ' . $zip . ' WHERE id = ' . $id;
	    $this->open();
	    $result = mysqli_query($con, $sql);
	    $this->close();
	    return $result;
    };// end function

    $deleteAddress = function($id) {
	    global $con;
	    $sql = 'DELETE FROM address WHERE id = ' . $id;
	    $this->open();
	    $result = mysqli_query($con, $sql);
	    $this->close();
	    return $result;
    };// end function

    $listAddress = function() {
	    global $con;
	    $sql = 'SELECT * FROM address';
	    $this->open();
	    $results = mysqli_query($con, $sql);
	    $this->close();
	    if ($results) {
		    $addresss = array();
		    while ($result = mysqli_fetch_array($results)) {
			    $address = new Address();
			    $address->setId($result[0]);
			    $address->setStreet1($result[1]);
			    $address->setStreet2($result[2]);
			    $address->setStreet3($result[3]);
			    $address->setCity($result[4]);
			    $address->setStateId($this->readState($result[5]));
			    $address->setZip($result[6]);
			    $addresss[] = $address;
		    };// end while
	    } else {
		    $addresss = false;
	    }
	    return $addresss;
    };// end function
